package shaka.ui

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.Button
import org.eclipse.swt.widgets.Text

import shaka.groovy.swt.SwtBuilder
import shaka.utils.SkUtils
import shaka.utils.face.ICallBack2

class SwtCodeGenPanel {
	private static FUNC = 'SwtCodeGenPanel'
	static panel(SwtBuilder swt,String fontType,int fontSize,fontStyle){
		Map w = [:]
		Closure saveCB = { String key ->
			Text text_src = w['1-text_src']
			Text text_target = w['1-text_target']
			Text text_arg = w['1-text_arg']
			Button check1 = w['1-check']

			Button check2 = w['2-check']
			Text text_path = w['2-1-text_path']
			Text text_index = w['2-1-1-text_index']
			Text text_tmpl_args = w['2-text_tmpl_args']
			Text text_tmpl = w['2-text_tmpl']

			Map saveArgs = [
				text_src       : text_src.text,
				text_target    : text_target.text,
				text_arg       : text_arg.text,
				check1         : check1.selection,
				check2         : check2.selection,
				text_path      : text_path.text,
				text_index     : text_index.text,
				text_tmpl_args : text_tmpl_args.text,
				text_tmpl      : text_tmpl.text
			]

			JsonBuilder jb = new JsonBuilder()
			jb(saveArgs)
			FuncStorage.instance.func(FUNC, key, jb.toString())
		}
		swt.cTabItem(c:[SWT.NONE], text:'程式碼產生器'){
			font(c:[fontType, fontSize, fontStyle])
			composite(c:[SWT.BORDER]){
				formLayout()
				group(c:[SWT.NONE], text:'參數設置'){
					formData(top:[0, 5], left:[0, 5], right:[100, -5], bottom:[40, 0])
					//font(c:[fontType, fontSize, fontStyle])
					formLayout()
					w['1-exec_btn'] = button(c:[SWT.PUSH], text:'執行'){
						formData(top:[0, -3], right:[100, -3])
						// 執行 - 程式碼產生
						event(click:{ codeGenerate(swt, w) })
					}
					label(c:[SWT.SEPARATOR | SWT.VIRTUAL]){
						formData(top:[0, 0], right:[w['1-exec_btn'], -3], bottom:[0, 20])
					}
					w['1-load_btn'] = button(c:[SWT.PUSH], text:'取出'){
						formData(top:[0, -3], right:[w['1-exec_btn'], -10])
						event(click:{
							SwtApplication.dialogGet(FUNC, { String value ->
								// get
								JsonSlurper js = new JsonSlurper()
								Map args = js.parseText(value)

								Text text_src = w['1-text_src']
								Text text_target = w['1-text_target']
								Text text_arg = w['1-text_arg']
								Button check1 = w['1-check']

								Button check2 = w['2-check']
								Text text_path = w['2-1-text_path']
								Text text_index = w['2-1-1-text_index']
								Text text_tmpl_args = w['2-text_tmpl_args']
								Text text_tmpl = w['2-text_tmpl']

								text_src.text = args.text_src
								text_target.text = args.text_target
								text_arg.text = args.text_arg
								check1.selection = new Boolean(args.check1)

								check2.selection = new Boolean(args.check2)
								text_path.text = args.text_path
								text_index.text = args.text_index
								text_tmpl_args.text = args.text_tmpl_args
								text_tmpl.text = args.text_tmpl
							}, { String key ->
								// save
								saveCB(key)
							}, { String key, String text->
								// rename
								FuncStorage.instance.rename(FUNC, key, text)
							}, { String key ->
								// delete
								FuncStorage.instance.delete(FUNC, key)
							})
						})
					}
					w['1-save_btn'] = button(c:[SWT.PUSH], text:'儲存'){
						formData(top:[0, -3], right:[w['1-load_btn'], 0])
						event(click:{ SwtApplication.dialogSave(saveCB) })
					}
					w['1-label'] = label(c:[SWT.NONE], text:'是否產生程式碼'){ formData(top:[0, 3], right:[w['1-save_btn'], -5]) }
					w['1-check'] = button(c:[SWT.CHECK]) { formData(top:[0, 3], right:[w['1-label'], -5]) }
					cTabFolder(c:[SWT.TOP]){
						formData(top:[0, 5], left:[0, 0], right:[100, 0], bottom:[100, 0])
						cTabItem(c:[SWT.NONE], text:'路徑設置'){
							font(c:[fontType, fontSize, fontStyle])
							composite(c:[SWT.BORDER]){
								formLayout()
								group(c:[SWT.NONE], text:'來源路徑'){
									formData(top:[0, 0], left:[0, 0], right:[100, 0], bottom:[50, 0])
									font(c:[fontType, fontSize, fontStyle])
									fillLayout()
									w['1-text_src'] = text(c:[SWT.BORDER | SWT.MULTI | SWT.V_SCROLL]){
										font(c:[fontType, fontSize, fontStyle])
										ctrla()
									}
								}
								group(c:[SWT.NONE], text:'目的路徑'){
									formData(top:[50, 0], left:[0, 0], right:[100, 0], bottom:[100, 0])
									font(c:[fontType, fontSize, fontStyle])
									fillLayout()
									w['1-text_target'] = text(c:[SWT.BORDER | SWT.MULTI | SWT.V_SCROLL]){
										font(c:[fontType, fontSize, fontStyle])
										ctrla()
									}
								}
							}
						}
						cTabItem(c:[SWT.NONE], text:'置換參數設置'){
							font(c:[fontType, fontSize, fontStyle])
							composite(c:[SWT.BORDER]){
								fillLayout()
								w['1-text_arg'] = text(c:[SWT.BORDER | SWT.MULTI | SWT.V_SCROLL]){
									font(c:[fontType, fontSize, fontStyle])
									ctrla()
								}
							}
						}
						selection(0)
					}
				}
				group(c:[SWT.NONE], text:'設定檔'){
					formData(top:[40, 0], left:[0, 5], right:[100, -5], bottom:[100, -5])
					//font(c:[fontType, fontSize, fontStyle])
					formLayout()
					w['2-label'] = label(c:[SWT.NONE], text:'是否產生設定檔'){ formData(top:[0, 3], right:[100, -5]) }
					w['2-check'] = button(c:[SWT.CHECK]) { formData(top:[0, 3], right:[w['2-label'], -5]) }
					cTabFolder(c:[SWT.TOP]){
						formData(top:[0, 5], left:[0, 0], right:[100, 0], bottom:[100, 0])
						cTabItem(c:[SWT.NONE], text:'設定'){
							font(c:[fontType, fontSize, fontStyle])
							composite(c:[SWT.BORDER]){
								formLayout()
								w['2-1-label'] = label(c:[SWT.NONE], text:'目的：'){
									font(c:[fontType, fontSize, fontStyle])
									formData(top:[0, 4], left:[0, 3])
								}
								w['2-1-btn'] = button(c:[SWT.PUSH], text:'選擇...'){
									font(c:[fontType, fontSize, fontStyle])
									formData(right:[100, -5])
								}
								w['2-1-text_path'] = text(c:[SWT.BORDER]){
									font(c:[fontType, fontSize, fontStyle])
									formData(top:[0, 3], left:[w['2-1-label'], 2], right:[w['2-1-btn'], -2])
									ctrla()
								}
								group(c:[SWT.NONE], text:'索引'){
									formData(top:[w['2-1-text_path'], 3], left:[0, 2], right:[100, -2], bottom:[100, 0])
									fillLayout()
									w['2-1-1-text_index'] = text(c:[SWT.BORDER | SWT.MULTI | SWT.V_SCROLL]){
										font(c:[fontType, fontSize, fontStyle])
										ctrla()
									}
								}
							}
						}
						cTabItem(c:[SWT.NONE], text:'置換參數設置'){
							font(c:[fontType, fontSize, fontStyle])
							composite(c:[SWT.BORDER]){
								fillLayout()
								w['2-text_tmpl_args'] = text(c:[SWT.BORDER | SWT.MULTI | SWT.V_SCROLL]){
									font(c:[fontType, fontSize, fontStyle])
									ctrla()
								}
							}
						}
						cTabItem(c:[SWT.NONE], text:'樣版'){
							font(c:[fontType, fontSize, fontStyle])
							composite(c:[SWT.BORDER]){
								fillLayout()
								w['2-text_tmpl'] = text(c:[SWT.BORDER | SWT.MULTI | SWT.V_SCROLL]){
									font(c:[fontType, fontSize, fontStyle])
									ctrla()
								}
							}
						}
						selection(0)
					}
				}
			}
		}
	}

	static codeGenerate(SwtBuilder swt, Map w){
		// 執行 程式碼產生
		Text text_src = w['1-text_src']
		Text text_target = w['1-text_target']
		Text text_arg = w['1-text_arg']
		Button check1 = w['1-check']

		Button check2 = w['2-check']
		Text text_path = w['2-1-text_path']
		Text text_index = w['2-1-1-text_index']
		Text text_tmpl_args = w['2-text_tmpl_args']
		Text text_tmpl = w['2-text_tmpl']

		if(check1.selection){
			if(!text_src.text)
				return
			if(!text_target.text)
				return
			if(!text_arg.text)
				return
		}
		if(check2.selection){
			if(!text_path.text)
				return
			if(!text_index.text)
				return
			if(!text_tmpl_args.text)
				return
			if(!text_tmpl.text)
				return
		}
		if(check1.selection){
			Map<String, String> args = SkUtils.str2Map(text_arg.text)
			List<String> srcs = SkUtils.str2List(text_src.text)
			List<String> targets = SkUtils.str2List(text_target.text)
			SkUtils.fileTmplRelativelyCopy(srcs, targets, args, args, [cb : {File src, File target ->
					StringBuffer code = new StringBuffer(SkUtils.fileRead(src))
					SkUtils.strTemplate(args, code, false)
					SkUtils.fileWrite(target, code.toString())
				}] as ICallBack2)
		}
		if(check2.selection){
			File configF = new File(new File(text_path.text), "config.xml")
			StringBuffer text = new StringBuffer()
			Map<String, String> pagenumMap = SkUtils.str2Map(text_index.text)
			Map<String, String> cfg_args = SkUtils.str2Map(text_tmpl_args.text)
			String config_tmpl = text_tmpl.text
			pagenumMap.each { String k, String v ->
				cfg_args.put("current_num", k)
				cfg_args.put("current_fn_name", v)

				StringBuffer sb = new StringBuffer(config_tmpl)
				SkUtils.strTemplate(cfg_args, sb)
				sb.append("\n\n")

				text.append(sb)
			}
			SkUtils.fileWrite(configF, text.toString())
		}
		swt.alert(title:'執行', message:'轉換完成')
	}
}
